# encoding: utf-8

module ApplicationHelper
  def var_to_js
    vars = self.instance_variables.select do |var|
      # var.to_s[1] != '_' &&
      ![:@_config, :@_routes,
        :@_assigns, :@_controller, :@_request,
        :@view_renderer, :@view_flow,
        :@output_buffer, :@virtual_path,
        :@asset_paths, :@javascript_include,
        :@stylesheet_include].include? var
    end
    vars.map do |var|
      var_s = var.to_s
      var_s[0] = '_'
      "window.#{var_s} = #{raw self.instance_variable_get(var).to_json}"
    end.join(";\n").html_safe
  end

  def date_str(add = 0)
    @_today ||= params[:date] ? Time.parse(params[:date]) : Time.now
    params[:date] ||= Time.now.strftime('%Y-%m-%d')
    @_today + add.to_i.days
  end

  def sns_type_tag(sns_type)
    _sns_type = ''
    case sns_type.to_s.downcase
    when "weibo"
      _sns_type = "新浪"
    when "tencent"
      _sns_type = "腾讯"
    when "163"
      _sns_type = "网易"
    end
    "<span class=\"label\">#{_sns_type}</span>".html_safe
  end
end
